package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.ProductModel;
import com.kcsj.domain.Warehouse;
import com.kcsj.service.ProductModelService;
import com.kcsj.service.WarehouseService;
import com.kcsj.util.Condition;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
@Slf4j
@WebServlet("/model")
public class ProductModelController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端请求的信息
        String productNo = request.getParameter("productNo");
        String productModelNo = request.getParameter("productModelNo");
        System.out.println(productNo);
        try {
            if(productNo!=null&&productModelNo==null){
                this.responseProductModelsFindByNo(response,productNo);
            }else if (productModelNo!=null){
                this.responseProductModelsNo(response,productModelNo);
            }else {
                this.responseProductModels(response,null);
            }
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
            e.printStackTrace();
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }

    private void responseProductModelsFindByNo(HttpServletResponse response, String no) throws SQLException, IOException {
        Collection<ProductModel> productModels = ProductModelService.getInstance().findByProductNo(no);
        String productModels_jsonstr = JSON.toJSONString
                (productModels, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(productModels_jsonstr);
        response.getWriter().println(data);
    }

    private void responseProductModelsNo(HttpServletResponse response, String no) throws SQLException, IOException {
        ProductModel productModels = ProductModelService.getInstance().find(no);
        String productModels_jsonstr = JSON.toJSONString
                (productModels, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(productModels_jsonstr);
        response.getWriter().println(data);
    }

    private void responseProductModelsNo(HttpServletResponse response, String no) throws SQLException, IOException {
        ProductModel productModels = ProductModelService.getInstance().find(no);
        String productModels_jsonstr = JSON.toJSONString
                (productModels, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(productModels_jsonstr);
        response.getWriter().println(data);
    }


    private void responseProductModels(HttpServletResponse response,
                                       List<Condition> conditionList)
            throws SQLException, IOException {
        Collection<ProductModel> productModels
                = ProductModelService.getInstance().findAll(conditionList);
        String productModels_jsonstr
                = JSON.toJSONString(productModels, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(productModels_jsonstr);
        //响应resp_jsonObj到前端
        response.getWriter().println(data);
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
            throws IOException, ServletException {
        //将request body转换为JSON字串
        String req_jsonStr = JSONUtil.getJSON(request);
        try {
            //根据请求反序列化生成对象
            ProductModel productModel = JSON.parseObject(req_jsonStr,ProductModel.class);
            //添加产品信息
            int id = ProductModelService.getInstance().add(productModel);
            JSONObject addMessage = MessageHelper.addMessage("增加成功");
            response.getWriter().println(addMessage);
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String request_jsonstr = JSONUtil.getJSON(request);
        try {
            ProductModel productModel = JSON.parseObject(request_jsonstr, ProductModel.class);
            //到数据库表修改Degree对象对应的记录
            boolean judge = ProductModelService.getInstance().update(productModel);
            if (judge) {
                JSONObject addmessage = MessageHelper.addMessage("修改成功");
                response.getWriter().println(addmessage);
            }else {
                JSONObject addmessage = MessageHelper.addMessage("修改异常");
                response.getWriter().println(addmessage);
            }
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request,
                            HttpServletResponse response)
            throws ServletException, IOException {
        //从JSONObject对象中读取键“no”的值（Java Object对象），
        String no_obj = request.getParameter("productModelNo");
        try {
            //到数据库表中删除对应的教师
            boolean judge = ProductModelService.getInstance().delete(no_obj);
            if (judge) {
                JSONObject addMessage = MessageHelper.addMessage("删除成功");
                response.getWriter().println(addMessage);
            }else {
                JSONObject addMessage = MessageHelper.addMessage("删除失败");
                response.getWriter().println(addMessage);
            }
        } catch (SQLException e) {
            JSONObject addMessage = MessageHelper.dateBaseError();
            response.getWriter().println(addMessage);
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        } catch (Exception e) {
            JSONObject addMessage = MessageHelper.netWordError();
            //打印异常栈，方便调试（要用log4j代替）
            log.error(e.getMessage());
        }
    }
}
